package com.student.wym.controller;

import com.alibaba.fastjson.JSONArray;
import com.student.wym.po.Exam;
import com.student.wym.po.Student;
import com.student.wym.service.IExamService;
import com.student.wym.service.IQuestionService;
import com.student.wym.service.IStudentService;
import com.student.wym.util.JSONResult;
import com.student.wym.util.PageList;
import com.student.wym.vo.CurrentExam;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;

    @Autowired
    private IStudentService studentService;

    @Autowired
    private IQuestionService questionService;

    @RequestMapping(value = "/goPage", method = RequestMethod.GET)
    public String goPage(HttpServletRequest request) {
        Exam exam = examService.findCurrentExam();
        CurrentExam currentExam = new CurrentExam();
        if (exam != null) {
            Integer studentTotal = studentService.findStudentNum(exam.getId());
            Integer radioTotal = questionService.findRadioNum();
            Integer checkTotal = questionService.findCheckNum();
            Integer judgeTotal = questionService.findJudgeNum();
            Integer ed = studentService.findEd(exam.getId());
            Integer pass = studentService.findPass(exam.getId());
            String passEd = pass + "/" + ed;
            currentExam = exam2currentExam(exam, studentTotal, radioTotal, checkTotal, judgeTotal);
            currentExam.setPass(passEd);
            currentExam.setExamState(exam.getState());
        }
        request.getSession().setAttribute("CURRENT_EXAM_SESSION", currentExam);
        return "current_exam";
    }

    @RequestMapping(value = "/goList", method = RequestMethod.GET)
    public String goList() {
        return "list_exam";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public PageList list() {
        PageList resultList = examService.findAll();
        return resultList;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult delete(int id) {
        try {
            examService.delete(id);
            return new JSONResult(0);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(1, "删除出现错误");
        }
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody //严格用vo来接收，此处为方便直接用po
    public JSONResult save(Exam exam) {
        Integer num = examService.findNot();
        if ((num != null) && (num != 0)) {
            return new JSONResult(1, "请结束其他考试后再新建");
        }
        try {
            /*exam.setExamTime(1);*/
            examService.saveOne(exam);
            return new JSONResult(0);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(1, "新增出现错误");
        }
    }

    @RequestMapping(value = "/endExam", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult endExam(HttpServletRequest request) {
        Exam currentExam = examService.findCurrentExam();
        if (currentExam != null) {
            examService.endCurrentExam();
            CurrentExam session_exam = (CurrentExam) request.getSession().getAttribute("CURRENT_EXAM_SESSION");
            session_exam.setExamState(2);
            request.getSession().setAttribute("CURRENT_EXAM_SESSION", session_exam);
            return new JSONResult(0, "本次考试已结束");
        } else {
            return new JSONResult(1, "当前没有考试");
        }

    }

    @RequestMapping(value = "/startExam", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult startExam(HttpServletRequest request) {
        Exam exam = examService.findCurrentExam2();
        if (exam != null) {
            examService.startCurrentExam();
            CurrentExam session_exam = (CurrentExam) request.getSession().getAttribute("CURRENT_EXAM_SESSION");
            session_exam.setExamState(1);
            request.getSession().setAttribute("CURRENT_EXAM_SESSION", session_exam);
            return new JSONResult(0, "本次考试成功开启");
        } else {
            return new JSONResult(1, "当前没有考试");
        }
    }

    @RequestMapping("/pieData")
    @ResponseBody
    public JSONResult pieData(HttpServletRequest request) {
        Exam exam = examService.findCurrentExam();
        JSONArray jsonArray = new JSONArray();
        if (exam != null) {
            Integer examId = exam.getId();
            List<Student> students = studentService.findAllByExamId(examId);
            if (CollectionUtils.isNotEmpty(students)) {
                Integer f0 = 0, f1 = 0, f2 = 0, a1 = 0, a2 = 0, a3 = 0; //难得想名字...
                for (int i = 0, size = students.size(); i < size; ++i) {
                    Student student = students.get(i);
                    if (student.getExamState() == 0) f0++;
                    if (student.getExamState() == 1) f1++;
                    if (student.getExamState() == 2) f2++;
                    if (student.getMakeUpState() == 1) a1++;
                    if (student.getMakeUpState() == 2) a2++;
                    if (student.getMakeUpState() == 3) a3++;
                }
                JSONArray jsonF0 = new JSONArray();
                jsonF0.add(0, "未考试");
                jsonF0.add(1, f0);
                jsonArray.add(jsonF0);
                if (f0 != students.size()) {
                    JSONArray jsonF1 = new JSONArray();
                    jsonF1.add(0, "正在初试");
                    jsonF1.add(1, f1);
                    jsonArray.add(jsonF1);
                    JSONArray jsonF2 = new JSONArray();
                    jsonF2.add(0, "初试已通过");
                    jsonF2.add(1, f2);
                    jsonArray.add(jsonF2);
                    JSONArray jsonA1 = new JSONArray();
                    jsonA1.add(0, "正在补考");
                    jsonA1.add(1, a1);
                    jsonArray.add(jsonA1);
                    JSONArray jsonA2 = new JSONArray();
                    jsonA2.add(0, "补考已通过");
                    jsonA2.add(1, a2);
                    jsonArray.add(jsonA2);
                    JSONArray jsonA3 = new JSONArray();
                    jsonA3.add(0, "补考未通过");
                    jsonA3.add(1, a3);
                    jsonArray.add(jsonA3);
                }
                return new JSONResult(jsonArray);
            } else {
                return new JSONResult();
            }

        } else {
            return new JSONResult();
        }
    }

    private CurrentExam exam2currentExam(Exam exam, Integer studentTotal, Integer radioTotal, Integer checkTotal, Integer judgeTotal) {
        CurrentExam currentExam = new CurrentExam();
        currentExam.setId(exam.getId());
        currentExam.setExamName(exam.getExamName());
        currentExam.setExamTime(exam.getExamTime());
        currentExam.setRadioNum(exam.getRadioNum());
        currentExam.setRadioScore(exam.getRadioScore());
        currentExam.setCheckNum(exam.getCheckNum());
        currentExam.setCheckScore(exam.getCheckScore());
        currentExam.setJudgeNum(exam.getJudgeNum());
        currentExam.setJudgeScore(exam.getJudgeScore());
        currentExam.setStudentTotal(studentTotal);
        currentExam.setRadioTotal(radioTotal);
        currentExam.setCheckTotal(checkTotal);
        currentExam.setJudgeTotal(judgeTotal);
        return currentExam;
    }

}
